package astri.pdcontrol;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;

import astri.pdcontrol.commclient.AstCommClientListener;
import astri.pdcontrol.commclient.AstCommMessage;
import astri.pdcontrol.client.PDControlClient;

/**
 * Test App for PDControlClient
 * @author hspoon @ASTRI
 *
 */
public final class testApp {
	final static boolean debug_on = true;
	//////util function //////
	private static void DEBUG(String msg) {
		if(!debug_on) 
			return;
		StackTraceElement e = Thread.currentThread().getStackTrace()[2];
		String fullClassName = e.getClassName();            
        String methodName = e.getMethodName();
        int lineNumber = e.getLineNumber();
		System.out.println("["+fullClassName + "." + methodName + "():" + lineNumber+"]\t " + msg);
	}
	
	private static void INFO(String msg) {
		StackTraceElement e = Thread.currentThread().getStackTrace()[2];
		String fullClassName = e.getClassName();            
        String methodName = e.getMethodName();
        int lineNumber = e.getLineNumber();
		System.out.println("["+fullClassName + "." + methodName + "():" + lineNumber+"]\t " + msg);
	}
	
	/**
	 * Custom AstCommClientListener
	 */
	public class MyListener implements AstCommClientListener {
		public MyListener()
		{
			DEBUG("create myListener");
		}
		@Override
		public int onConnect(Socket sock) {
			DEBUG("Get onConnect event, sock = " + sock.getInetAddress() + ":" + sock.getPort());
			return 0;
		}

		@Override
		public int onConnectNonBlock(boolean connected,
				InetSocketAddress sockAddr) {
			DEBUG("Get onConnectNonBlock event, connected? " + connected +" on " + sockAddr);
			return 0;
		}

		@Override
		public int onDisconnect(Socket sock) {
			DEBUG("Get onDisconnect event, sock = " + sock.getInetAddress() + ":" + sock.getPort());
			return 0;
		}

		@Override
		public int onDiscovery(InetSocketAddress sockAddr) {
			DEBUG("Get onDiscovery event, sockAddr = "+ sockAddr);
			return 0;
		}

		@Override
		public int onReceive(Socket sock, AstCommMessage msg) {
			DEBUG("Get onReceive event from sock = " + sock.getInetAddress() + ":" + sock.getPort());
			DEBUG(String.format("Msg magic= 0x%x",msg.header.magic));
			DEBUG(String.format("Msg id= 0x%x",msg.header.id));
			DEBUG("Msg length= " + msg.header.length);
			return 0;
		}
		
	}
	
	/**
	 * TestApp for AstCommClient
	 */
	public static void main(String[] args){ 
		testApp a = new testApp(); 
		testApp.MyListener listener =  a.new MyListener();
        PDControlClient client = new PDControlClient(3000, 3000, listener);
        client.startService();
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        String input;
        
        System.out.println("Test App for PDControlClient >>> ");
        do {
        	try {
	        	System.out.print("command: ");
		        input = inFromUser.readLine();
		        
		        if(input.equalsIgnoreCase("quit")) {
		        	client.disconnect();
		        	break;
		        }
		        else if(input.equalsIgnoreCase("connect")) {
		        	System.out.print("IP: ");
		        	String ip = inFromUser.readLine();
		        	System.out.print("Port: ");
		        	int port = Integer.parseInt(inFromUser.readLine());
		        	if(client.connectTo(ip, port) == false)
		            {
		        		INFO("connect fail!");
		            }
		        }
		        else if(input.equalsIgnoreCase("disconnect")) {
		        	client.disconnect();
		        }
		        else if(input.equalsIgnoreCase("discovery")) {
		        	client.startDiscoveryServer(5000);
		        }
		        else if(input.equalsIgnoreCase("sendCursor")) {
		        	// Example of How to Send a series of Cursor Data
		        	 DEBUG(" ----> ----> Cursor Data Out");
		             client.sendCursorStart();
		             for (int i=0; i<5; i++)
		             {
		         		client.sendCursorData(10, -10);
		             }
		             client.sendCursorFinish();
		             DEBUG(" ----> ----> Cursor Data Complete");
		        }
		        else if(input.equalsIgnoreCase("sendAcc")) {
		        	// Example of How to Send a series of Accelerometer Data
		        	DEBUG(" ----> ----> Accelerometer Data Out");
		            client.sendAccStart();
		            for (int i=0; i<5; i++)
		            {
		        		client.sendAccData(8, 88, 888);
		            }
		            client.sendAccFinish();
		            DEBUG(" ----> ----> Accelerometer Data Complete");
		        }
		        else if(input.equalsIgnoreCase("sendText")) {
		        	 // Example of How to Send a series of Text Data
		        	DEBUG(" ----> ----> Text Data Out");
		            client.sendTxtStart();
		            for (int i=0; i<3; i++)
		            {
		        		client.sendTxtData("Test String Comes Here!");
		            }
		            client.sendTxtFinish();
		            DEBUG(" ----> ----> Text Data Complete");
		        }
		        else if(input.equalsIgnoreCase("sendScroll")) {
		        	// Example of How to Send Scroll Data
			        DEBUG(" ----> ----> Scroll Data Out");
			        for (int i=0; i<10; i++)
			        {
			        	client.sendScrollData(i*10);
			        }
			        DEBUG(" ----> ----> Scroll Data Complete");
		        }
		        else if(input.equalsIgnoreCase("sendKey")) {
		        	// Example of How to Send a discrete Commands
		        	DEBUG(" ----> ----> Commands Out");
		            client.sendQtKey(0x010000a2); //PD_CONTROL_KEY_SINGLE_TAP
		            client.sendQtKey(0x010000a3); //PD_CONTROL_KEY_DOUBLE_TAP
		            client.sendQtKey(0x010000a4); //PD_CONTROL_KEY_MULTITOUCH
		            client.sendQtKey(0x010000a5); //PD_CONTROL_KEY_FLING_LEFT
		            client.sendQtKey(0x010000a6); //PD_CONTROL_KEY_FLING_RIHGT
		            client.sendQtKey(0x010000a7); //PD_CONTROL_KEY_FLING_UP
		            client.sendQtKey(0x010000a8); //PD_CONTROL_KEY_FLING_DOWN
		            client.sendQtKey(0x010000a9); //PD_CONTROL_KEY_SHAKE_CMD
		            DEBUG(" ----> ----> Commands Complete");
		        }
		        else {
		        	System.out.println("=========command list=========");
		        	System.out.println("quit               : quit App");
		        	System.out.println("connect            : connect a socket");
		        	System.out.println("disconnect         : disconnect current socket");
		        	System.out.println("discovery          : start discovery Server");
		        	System.out.println("sendCursor         : send cursor data to server");
		        	System.out.println("sendAcc            : send Acceleometer data to server");
		        	System.out.println("sendText           : send text data to server");
		        	System.out.println("sendScroll         : send scroll data to server");
		        	System.out.println("sendKey            : send key data to server");        	
		        	System.out.println("==============================");
		        }
        	} catch (Exception e) {
        		e.printStackTrace();
			}
        } while (true);
        client.stopService();
        DEBUG("Test App closed");
    }
}
